#!/bin/bash

set -o errexit
set -o xtrace

test_dir=$(realpath $(dirname $0))
. ${test_dir}/../functions

newpass="test-password"
newpassencrypted=$(echo -n "$newpass" | base64)

create_namespace $namespace
deploy_operator

deploy_minio

psmdb="some-name"
cluster="some-name-rs0"

kubectl_bin apply -f "${conf_dir}/client.yml" \
    -f "${conf_dir}/secrets.yml" \
    -f "${conf_dir}/cloud-secret.yml" \
    -f "${conf_dir}/minio-secret.yml"

apply_cluster $test_dir/conf/$cluster.yml
desc 'Check if all 3 Pods started'
wait_for_running $cluster 3

backup_name_aws="backup-aws-s3"

desc 'Change MONGODB_BACKUP_PASSWORD'
patch_secret "some-users" "MONGODB_BACKUP_PASSWORD" "$newpassencrypted"
sleep 25
wait_cluster_consistency $psmdb
sleep 15
user=$(getSecretData "some-users" "MONGODB_BACKUP_USER")
check_mongo_auth "$user:$newpass@$cluster-0.$cluster.$namespace"
check_mongo_auth "$user:$newpass@$cluster-1.$cluster.$namespace"
check_mongo_auth "$user:$newpass@$cluster-2.$cluster.$namespace"

desc 'Change MONGODB_BACKUP_USER'
newnameencrypted=$(echo -n "backup2" | base64)
patch_secret "some-users" "MONGODB_BACKUP_USER" "$newnameencrypted"
sleep 25

wait_cluster_consistency $psmdb
sleep 15
user=$(getSecretData "some-users" "MONGODB_BACKUP_USER")
check_mongo_auth "$user:$newpass@$cluster-0.$cluster.$namespace"
check_mongo_auth "$user:$newpass@$cluster-1.$cluster.$namespace"
check_mongo_auth "$user:$newpass@$cluster-2.$cluster.$namespace"

desc 'Run backup'
run_backup aws-s3
wait_backup "$backup_name_aws"

desc 'Change MONGODB_USER_ADMIN_PASSWORD'
patch_secret "some-users" "MONGODB_USER_ADMIN_PASSWORD" "$newpassencrypted"
sleep 25
wait_cluster_consistency $psmdb
sleep 15
user=$(getSecretData "some-users" "MONGODB_USER_ADMIN_USER")
check_mongo_auth "$user:$newpass@$cluster-0.$cluster.$namespace"
check_mongo_auth "$user:$newpass@$cluster-1.$cluster.$namespace"
check_mongo_auth "$user:$newpass@$cluster-2.$cluster.$namespace"

desc 'Change MONGODB_USER_ADMIN_USER'
newnameencrypted=$(echo -n "userAdmin2" | base64)
patch_secret "some-users" "MONGODB_USER_ADMIN_USER" "$newnameencrypted"
sleep 25
wait_cluster_consistency $psmdb
sleep 15
user=$(getSecretData "some-users" "MONGODB_USER_ADMIN_USER")
check_mongo_auth "$user:$newpass@$cluster-0.$cluster.$namespace"
check_mongo_auth "$user:$newpass@$cluster-1.$cluster.$namespace"
check_mongo_auth "$user:$newpass@$cluster-2.$cluster.$namespace"

desc 'Change MONGODB_CLUSTER_ADMIN_PASSWORD'
patch_secret "some-users" "MONGODB_CLUSTER_ADMIN_PASSWORD" "$newpassencrypted"
sleep 25
wait_cluster_consistency $psmdb
sleep 15
user=$(getSecretData "some-users" "MONGODB_CLUSTER_ADMIN_USER")
check_mongo_auth "$user:$newpass@$cluster-0.$cluster.$namespace"
check_mongo_auth "$user:$newpass@$cluster-1.$cluster.$namespace"
check_mongo_auth "$user:$newpass@$cluster-2.$cluster.$namespace"

desc 'Change MONGODB_CLUSTER_MONITOR_PASSWORD'
patch_secret "some-users" "MONGODB_CLUSTER_MONITOR_PASSWORD" "$newpassencrypted"
sleep 25
wait_cluster_consistency $psmdb
sleep 15
user=$(getSecretData "some-users" "MONGODB_CLUSTER_MONITOR_USER")
check_mongo_auth "$user:$newpass@$cluster-0.$cluster.$namespace"
check_mongo_auth "$user:$newpass@$cluster-1.$cluster.$namespace"
check_mongo_auth "$user:$newpass@$cluster-2.$cluster.$namespace"

desc 'Remove users secret'
kubectl_bin delete secret some-users
sleep 25
wait_cluster_consistency $psmdb
sleep 15
user=$(getSecretData "some-users" "MONGODB_USER_ADMIN_USER")
pass=$(getSecretData "some-users" "MONGODB_USER_ADMIN_PASSWORD")
check_mongo_auth "$user:$pass@$cluster-0.$cluster.$namespace"
check_mongo_auth "$user:$pass@$cluster-1.$cluster.$namespace"
check_mongo_auth "$user:$pass@$cluster-2.$cluster.$namespace"

desc 'Update all users'
kubectl_bin apply -f "${conf_dir}/secrets.yml"
sleep 25
wait_cluster_consistency $psmdb
sleep 15
user=$(getSecretData "some-users" "MONGODB_USER_ADMIN_USER")
pass=$(getSecretData "some-users" "MONGODB_USER_ADMIN_PASSWORD")
check_mongo_auth "$user:$pass@$cluster-0.$cluster.$namespace"
check_mongo_auth "$user:$pass@$cluster-1.$cluster.$namespace"
check_mongo_auth "$user:$pass@$cluster-2.$cluster.$namespace"

desc 'Secret without userAdmin'
kubectl_bin apply -f "${test_dir}/conf/secrets.yml"
sleep 25
check_mongo_auth "$user:$pass@$cluster-0.$cluster.$namespace"
check_mongo_auth "$user:$pass@$cluster-1.$cluster.$namespace"
check_mongo_auth "$user:$pass@$cluster-2.$cluster.$namespace"

destroy $namespace
